import {describe} from 'mocha'
import * as interaction from '../../src/interaction'
import * as outputs from '../../src/outputs'
import sinon from 'sinon'
import {Action, DEFAULT_ORDER} from '../../src/interaction'
import {appendSummaryInfo} from '../../src/interaction/summary'

describe('测试', () => {
    let sandbox: sinon.SinonSandbox
    let addOutputStub: sinon.SinonStub
    beforeEach(() => {
        sandbox = sinon.createSandbox()
        addOutputStub = sandbox.stub(outputs, 'addOutput')
    })

    afterEach(() => {
        sandbox.restore()
    })

    it('测试runAction', () => {
        interaction.runAction('CHECK')
        sinon.assert.calledWith(addOutputStub, `SYSTEM_AGENTLESS_ACTION=CHECK`)
    })

    it('测试appendAction', () => {
        const actions: interaction.Action[] = []
        const action: Action = {
            type: 'EXECUTE',
            name: 'EXECUTE',
            order: 1,
            action: 'CHECK'
        }
        actions.push(action)
        interaction.appendActions(actions)
        sinon.assert.calledWith(
            addOutputStub,
            `SYSTEM_ACTIONS=[{"type":"EXECUTE","name":"EXECUTE","title":"EXECUTE","action":"CHECK","disable":false,"hidden":false,"metadata":{"order":1}}]`
        )
    })

    it('测试 append hidden Action ', () => {
        const actions: interaction.Action[] = []
        const action: Action = {
            type: 'HIDDEN',
            name: '隐藏按钮',
            order: 1,
            action: 'CHECK'
        }
        actions.push(action)
        interaction.appendActions(actions)
        sinon.assert.calledWith(
            addOutputStub,
            `SYSTEM_ACTIONS=[{"type":"HIDDEN","name":"隐藏按钮","title":"隐藏按钮","action":"CHECK","disable":false,"hidden":true,"metadata":{"order":1}}]`
        )
    })

    it('测试message', () => {
        interaction.appendMessage('等待调度')
        sinon.assert.calledWith(addOutputStub, `SYSTEM_MESSAGE=等待调度`)
    })

    it('test appendErrorMessage', () => {
        interaction.appendErrorMessage('Error1')
        sinon.assert.calledWith(addOutputStub, `ERR_INFO: Error1`)
    })

    it('test link', () => {
        interaction.addLink('test', 'https://www.aliyun.com')
        sinon.assert.calledWith(
            addOutputStub,
            `BUTTONS=[{"disable":false,"type":"LINK","name":"test","title":"test","url":"https://www.aliyun.com","order":${DEFAULT_ORDER}}]`
        )
    })

    it('test link with order', () => {
        interaction.addLink('test', 'https://www.aliyun.com', 42)
        sinon.assert.calledWith(
            addOutputStub,
            `BUTTONS=[{"disable":false,"type":"LINK","name":"test","title":"test","url":"https://www.aliyun.com","order":42}]`
        )
    })
})

describe('appendSummaryInfo', () => {
    let sandbox: sinon.SinonSandbox
    let addOutputStub: sinon.SinonStub
    beforeEach(() => {
        sandbox = sinon.createSandbox()
        addOutputStub = sandbox.stub(outputs, 'addOutput')
    })

    afterEach(() => {
        sandbox.restore()
    })

    it('should not call addOutput if title or message is empty', () => {
        appendSummaryInfo(' ', 'This is a message')
        appendSummaryInfo('This is a title', ' ')
        appendSummaryInfo('', '')
        appendSummaryInfo('This is a title', '')

        sinon.assert.notCalled(addOutputStub)
    })

    it('should call addOutput with the correct summary info', () => {
        const title = 'Test Title'
        const message = 'Test Message'
        appendSummaryInfo(title, message)

        sinon.assert.calledWith(
            addOutputStub,
            `SUMMARY_INFO={"message":"${message}","title":"${title}"}`
        )
    })
})
